<?php
namespace app\api\controller;
use think\Db;
use think\Image;
use app\api\controller\Base;


class Adds //extends Base
{
  public function phone()
  {
    $uid=$this->uid;
    return $this->AjaxReturn('1','已绑定手机号');
  }
  /*
     * 方法作用：发布产品的添加
     * @param
     * */
  public function adds(){
    $uid=$this->uid;
    $sql=db('user')
      ->where("uid",$uid)
      ->field('uflg')
      ->find();
    if ($sql['uflg']==0){
      return $this->AjaxReturn("123","黑名单无权操作");
    }
    $times=date('Y-m-d 00:00:00');
    $timee=date('Y-m-d H:i:s');
    $where ['createtime'] = array("between",array($times,$timee));
    $where['uid']=$uid;
    $res=db('message')
      ->where($where)
      ->count();
    $k=db('shezhi')
      ->find();
    $rs=db('real')
      ->where("uid",$uid)
      ->field('rid,rflg')
      ->find();
    if($rs['rflg']!="1"&&$res>=$k['userwenzhang']){
      return $this->AjaxReturn("12","今日超出发布次数");
    }
    if($rs['rflg']=="1"&&$res>=$k['vipwenzhang']){
      return $this->AjaxReturn("22","今日超出发布次数");
    }                                
    $timelimit=input('timelimit');
    $value=input("timelimit")*3600*24+time();
    $val=date("Y-m-d H:i:s",$value);
    $date['title']=input('title');
    $date['content']=input('content');
    $date['createtime']=date("Y-m-d H:i:s");
    $date['stoptime']='4000-01-04 23:59:59';
    $date['address']=input('address/a');
    $date['gid']=input('gid');
    $date['address'] = implode('',$date['address']);
    $date['uid']=$uid;
    $date['type']=1;
    $date['number']=input('number');

    $date['price']=input('price');

    if(empty($date['price'])){
      $date['price']="商议"; 
    }
    $date['unit']=intval(input('unit'));
    $date['timelimit']=input('timelimit');
    $date['usered']=$uid;
    $add=db('message')
      ->insertGetId($date);
    if ($add){
      return $this->AjaxReturn('1','发布成功',$add);
    }else{
      return $this->AjaxReturn('0','发布失败');
    }
  }

  /*
     * 方法作用：发布文章的添加
     * @param
     * */
  public function Based()
  {

    $file = request()->file('file');
    $mid=input('mid');
    $filename=md5(time().rand(1,1000));
    $fileRoute = ROOT_PATH . 'upload/message/'; 
    $info = $file->move($fileRoute,$filename);
    if (!$info){
      return $this->AjaxReturn('10','失败',1);
    }

    $type = input('type');
    if($mid==''){
      $where['type']=$type;
      $where['picture']=$info->getFilename().',';
      //$where['createtime']=date("Y-m-d H:i:s");
      $arr=db('message')
        ->insertGetid($where);

      return $this->AjaxReturn('1','操作成功',[$arr,$info->getFilename()]);	
    }else{

      $res= db('message')
        ->where('mid',$mid)
        ->field('picture')
        ->find();
      $where['picture']= $res['picture'].$info->getFilename().',';

      $arr= db('message')
        ->where('mid',$mid)
        ->update($where);
    }
    if($arr){
      return $this->AjaxReturn('1','操作成功',[$mid,$info->getFilename()]);
    }else{
      return $this->AjaxReturn('1','操作失败',$mid);
    }
  }




  //选择图片压缩并返回
  public function yasuo()
  {
    //接收最新的图片
    $image=input('image');
    $image=explode(',',$image);
    $image=array_filter($image);
    if(count($image)==0){
      return $this->AjaxReturn('-1','上传失败,请重试');
    }
    //服务器中存放图片的文件夹名称
    $fan_img = array();
    foreach($image as $k=>$v){
      $path='message/';
      //生成一个随机数用来生成图片名称
      $rand=rand(1000,9999);
      //生成图片名称
      $pers=md5(time().$rand).'.jpg';
      //文件夹目录
      $path ='upload'.DS.$path;
      //文件绝对路径
      $absolute=ROOT_PATH.$path.DS.$pers;

      //判断目录是否存在 不存在就创建
      if (!is_dir(ROOT_PATH.$path))
      {
        mkdir(ROOT_PATH.$path,0777,true);
      } 

      //将图片上传到服务器上
      $r = file_put_contents($absolute, base64_decode($v));
      $image = Image::open($absolute);
      $pic = $image->thumb(750,1000,Image:: THUMB_SCALING)->save($absolute);
      if($r){
        //'https://'.$_SERVER['SERVER_NAME'].'/'.$path.
        $fan_img[$k] = $pers;
      }
      $path = "";
    }
    if(count($fan_img)>0){
      return $this->AjaxReturn('1','成功',$fan_img);
    }else{
      return $this->AjaxReturn('-1','上传失败,请重试');
    }
  }

  //点击X删除图片
  public function del_img_btn(){
    $img_name = input('img_name');
    $path='message/';
    //文件夹目录
    $path ='upload'.DS.$path;
    //文件绝对路径
    $absolute=ROOT_PATH.$path.DS.$img_name;
    unlink($absolute);
  }
  public function BasePlus()
  {
    $mid=input('mid');
    //接收用户uid
    $uid=$this->uid;
    //接收最新的图片
    $image=input('image/a');
    $data = implode(',',$image).',';
    $arr= db('message')
      ->where('mid',$mid)
      ->update([
        'picture' => $data
      ]);
    if($arr){
      return $this->AjaxReturn('1','成功');
    }else{
      $del=db('message')->where('mid',$mid)->delete();
      return $this->AjaxReturn('-1','图片上传失败');
    }
  }
  //Base64图片上传
  public function Base()
  {
    $mid=input('mid');
    //接收用户uid
    $uid=$this->uid;
    //接收最新的图片
    $image=input('image');
    $image=explode(',',$image);
    $image=array_filter($image);
    if(count($image)==0){
      $del=db('message')->where('mid',$mid)->delete();
      return $this->AjaxReturn('-1','图片上传失败');
    }
    $fan_img = array();
    foreach($image as $k=>$v){
      //服务器中存放图片的文件夹名称
      $path='message/';
      //生成一个随机数用来生成图片名称
      $rand=rand(1000,9999);
      //生成图片名称
      $pers=md5(time().$rand).'.jpg';
      //文件夹目录
      $path ='upload'.DS.$path;
      //文件绝对路径
      $absolute=ROOT_PATH.$path.DS.$pers;
      //判断目录是否存在 不存在就创建
      if (!is_dir(ROOT_PATH.$path))
      {
        mkdir(ROOT_PATH.$path,0777,true);
      } 
      $res=db('message')
        ->where('mid',$mid)
        ->field('picture')
        ->find();
      $where['picture']= $res['picture'].$pers.',';
      $arr= db('message')
        ->where('mid',$mid)
        ->update($where);
      //将图片上传到服务器上
      $r = file_put_contents($absolute, base64_decode($v));
      $image = Image::open($absolute);
      $pic = $image->thumb(750,1000,Image:: THUMB_SCALING)->save($absolute);
      $fan_img[$k] = $absolute;
      //photoCompress($absolute);
      if(!$r){
        $del=db('message')->where('mid',$mid)->delete();
        return $this->AjaxReturn('-1','图片上传失败');
      }
    }
    if($r){
      return $this->AjaxReturn('1','成功',$fan_img);
    }else{
      $del=db('message')->where('mid',$mid)->delete();
      return $this->AjaxReturn('-1','图片上传失败');
    }
  }

  public function addwenzhang(){

    $uid=$this->uid;
    $sql=db('user')
      ->where("uid",$uid)
      ->find();
    if ($sql['uflg']==0){
      return $this->AjaxReturn("0","黑名单无权操作");
    }
    $times=date('Y-m-d 00:00:00');
    $timee=date('Y-m-d H:i:s');
    $where ['createtime'] = array("between",array($times,$timee));
    $where['uid']=$uid;
    $res=db('message')
      ->where($where)
      ->count();
    //return $this->AjaxReturn("22","今日超出发布次数",$res);
    $rs=db('real')
      ->where("uid",$uid)
      ->field('rflg')
      ->find();
    //return $this->AjaxReturn("22","今日超出发布次数",$rs);
    $k=db('shezhi')
      ->find();//return $this->AjaxReturn("22","今日超出发布次数",$k['vipwenzhang']);
    if($rs['rflg']!="1"&&$res>=$k['userwenzhang']){
      return $this->AjaxReturn("12","今日超出发布次数");
    }
    if($rs['rflg']=="1"&&$res>=$k['vipwenzhang']){
      return $this->AjaxReturn("22","今日超出发布次数");
    }                                
    $date['timelimit']=input('timelimit');
    $value=input("timelimit")*3600*24+time();
    $date['title']=input('title');
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx6c1a86ee3e43e4a3&secret=32f54b415ab46d898b2714c327b8d4b9";
    // 创建一个新cURL资源
    $ch  =  curl_init ();
    // 设置URL和相应的选项
    curl_setopt ( $ch , CURLOPT_URL ,  $url );
    curl_setopt ( $ch , CURLOPT_HEADER ,  false );
    curl_setopt ( $ch , CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ( $ch , CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt ( $ch , CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt ( $ch , CURLOPT_SSLVERSION, 1);
    // 抓取URL并把它传递给浏览器
    $str = curl_exec ( $ch );
    //关闭cURL资源，并且释放系统资源
    curl_close ( $ch );
    //将str->数组
    $arr = json_decode($str , true);
    $url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=".$arr['access_token'];
    $content=$date['title'];
    $file_data = '{ "content":"'.$content.'" }';//$content(需要检测的文本内容，最大520KB)
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch , CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $file_data);
    $output = curl_exec($ch);//发送请求获取结果
    curl_close($ch);//关闭会话
    $output=json_decode($output,true);
    if($output['errmsg']!="ok")
    {
    	return $this->AjaxReturn('0','您填写的标题里含有敏感词汇，请删除后重新发布');
    }
    $date['content']=input('content');
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx6c1a86ee3e43e4a3&secret=32f54b415ab46d898b2714c327b8d4b9";
    // 创建一个新cURL资源
    $ch  =  curl_init ();
    // 设置URL和相应的选项
    curl_setopt ( $ch , CURLOPT_URL ,  $url );
    curl_setopt ( $ch , CURLOPT_HEADER ,  false );
    curl_setopt ( $ch , CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ( $ch , CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt ( $ch , CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt ( $ch , CURLOPT_SSLVERSION, 1);
    // 抓取URL并把它传递给浏览器
    $str = curl_exec ( $ch );
    //关闭cURL资源，并且释放系统资源
    curl_close ( $ch );
    //将str->数组
    $arr = json_decode($str , true);
    $url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=".$arr['access_token'];
    $content=$date['content'];
    $file_data = '{ "content":"'.$content.'" }';//$content(需要检测的文本内容，最大520KB)
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch , CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $file_data);
    $output = curl_exec($ch);//发送请求获取结果
    curl_close($ch);//关闭会话
    $output=json_decode($output,true);
    if($output['errmsg']!="ok")
    {
    	return $this->AjaxReturn('0','您填写的文字里含有敏感词汇，请删除后重新发布');
    }
    $date['createtime']=date("Y-m-d H:i:s");
    $date['stoptime']="3019-09-09 23:59:59";
    $date['uid']=$uid;
    $date['type']=2;
    $date['price']='';
    $date['unit']='';
    $date['number']='';
    $date['faxian']=0;
    $date['address']=input('address');
    $date['timelimit']=input('timelimit');
    $date['usered']=$uid;
    $add=db('message')
      ->insertGetId($date);
    if ($add){
      return $this->AjaxReturn('1','发布成功',$add);
    }else{

      return $this->AjaxReturn('0','发布失败');
    }
  }


  /*
     * 方法作用：行情资讯搜索
     * @param $whole string 查询条件
     * @return code msg array[]   状态码  提示信息 返回数据
     * */
  public function InformationSee(){
    $whole=input('text');
    $where ['title'] = array("like","%".$whole."%");
    $wheres ['content'] = array("like","%".$whole."%");
    $sql=db('zixun')
      ->where($where)
      ->whereOr($wheres)
      ->where('flg',0)
      ->order('createtime desc')
      ->select();
    //print_r($sql);die;
    foreach ($sql as $key=>$value){
      $value['prctures']='https://www.agrimanu.cn' .$value['picture'];
      $arr[]=$value;

    }
    if (!empty($sql)){
      return $this->AjaxReturn("1","操作成功",$arr);
    }else{
      return $this->AjaxReturn("-1","操作失败");
    }
  }
  /*
     *方法作用：行情资讯详细页面
     * @param $id int 接收过来的文章ID
     *@param $where array 查询条件
     * @return code msg array[]   状态码  提示信息 返回数据
     * */
  public function InformationCheck(){
    $uid=$this->uid;
    $zid=input('id');//dump($zid);die;
    $rs=db('real')
      ->where('uid',$uid)
      ->field('rflg,tid')
      ->find();
    $re=db('user')
      ->where('uid',$uid)
      ->field('gflg')
      ->find();
    if ($rs['rflg']==1||$re['gflg']==1)
    {
      $check=db('zixun')
        ->where('zid',$zid)
        ->find();
      $check['picture']='https://www.agrimanu.cn'. $check['picture'];
      //浏览量加1
      $num=db('zixun')->where('zid',$zid)->field('id')->find();
      $arr8=array_filter(explode(',',$num['id']));
      if(!in_array($uid,$arr8))
      {
        $num1=$num['id'].$uid.',';
        $update=db('zixun')->where('zid',$zid)->update(['id'=>$num1]);
      }
      if (!empty($check))
      {
        return $this->AjaxReturn("1","操作成功",[$check,$rs]);
      }
      else
      {
        return $this->AjaxReturn("0","操作失败");
      }
    }
    else
    {
      $res=db('zpay')
        ->where('uid',$uid)
        ->where('zid',$zid)
        ->field('flg')
        ->find();
      if($res['flg']=='已支付')
      {
        $check=db('zixun')
          ->where('zid',$zid)
          ->find();//dump($check);die;
        $check['picture']='https://www.agrimanu.cn'. $check['picture'];
        $num=db('zixun')->where('zid',$zid)->field('id')->find();
        $arr8=array_filter(explode(',',$num['id']));
        //return $arr8;die;
        if(!in_array($uid,$arr8))
        {
          $num1=$num['id'].$uid.',';
          $update=db('zixun')->where('zid',$zid)->update(['id'=>$num1]);
        }
        if (!empty($check))
        {
          return $this->AjaxReturn("1","操作成功",[$check,$rs]);
        }
        else
        {
          return $this->AjaxReturn("0","操作失败");
        }
      }
      else
      {
        $check=db('zixun')
          ->where('zid',$zid)
          ->find();
        $check['picture']='https://www.agrimanu.cn'. $check['picture'];
        return $this->AjaxReturn("22","无权查看",[$check,$rs]);
      }
    }

  }
  /*
     *方法作用：委托采购信息
     * @param $whole int 咨询或者在线预约
     * @return code msg array[]   状态码  提示信息 返回数据
     * */
  public function EntrustPurchase(){
    $sql=db('caigous')
      ->select();
    if (!empty($sql)){
      return $this->AjaxReturn("1","操作成功",$sql);
    }else{

      return $this->AjaxReturn("1","操作成功");
    }
  }
  /*
     *方法作用：点击委托采购
     * @param $whole int 咨询或者在线预约
     * @return code msg array[]   状态码  提示信息 返回数据
     * */
  public function Entrus(){
    $uid=$this->uid;
    $flg=input('flg');
    $where['uid']=$uid;
    $where['flg']=$flg;
    $where['createtime']=date("Y-m-d H:i:s") ;
    $sr=db('user')
      ->where('uid',$uid)
      ->find();
    if($sr['uflg']==0){
      return $this->AjaxReturn('123','黑名单无权操作');
    }
    $sql=db('caigou')
      ->insert($where);
    if (!empty($sql)){
      return $this->AjaxReturn('1',"操作成功");
    }else{
      return $this->AjaxReturn('0',"操作失败");
    }
  }
  /*
     * 方法作用 ：金融服务随你呀部分
     * @param $whole int 咨询或者在线预约
     * @param $sql array[] 随你压信息
     * @return code msg array[]   状态码  提示信息 返回数据
     */
  public function Financialsuiya(){
    $sql=db('suiyas')
      ->select();

    if (!empty($sql)){
      return $this->AjaxReturn("1","操作成功",$sql);
    }else{
      return $this->AjaxReturn("0","操作失败");
    }
  }
  /*
     * 方法作用 ：金融服务帮你踩显示部分
     * @param $whole int 咨询或者在线预约
     * @param $res array[] 帮你踩信息
     * @return code msg array[]   状态码  提示信息 返回数据
     */
  public function Financialbangcai(){
    $sql=db('bangcais')
      ->select();
    if (!empty($sql)){
      return $this->AjaxReturn("1","操作成功",$sql);
    }else{
      return $this->AjaxReturn("0","操作失败");
    }
  }
  /*
     * 方法作用 ：点击随你呀
     * @param $whole int 咨询或者在线预约
     * @param $sql array[] 随你压信息
     * @param $res array[] 帮你踩信息
     * @return code msg array[]   状态码  提示信息 返回数据
     */
  public function suiniya(){
    $uid=$this->uid;
    $where['uid']=$uid;
    $sr=db('user')
      ->where('uid',$uid)
      ->find();
    if($sr['uflg']==0){
      return $this->AjaxReturn('123','黑名单无权操作');
    }
    $where['createtime']=date("Y-m-d H:i:s");
    $where['flg']=input('flg');
    $res=db('suiya')
      ->insert($where);
    if(!empty($res)){
      return $this->AjaxReturn("1","操作成功");
    }else{
      return $this->AjaxReturn("0","操作失败");
    }
  }
  /*
    * 方法作用 ：点击帮你踩
    * @param $whole int 咨询或者在线预约
    * @param $sql array[] 随你压信息
    * @param $res array[] 帮你踩信息
    * @return code msg array[]   状态码  提示信息 返回数据
    */
  public function bangnicai(){
    $uid=$this->uid;
    $where['uid']=$uid;
    $sr=db('user')
      ->where('uid',$uid)
      ->find();
    if($sr['uflg']==0){
      return $this->AjaxReturn('123','黑名单无权操作');
    }
    $where['createtime']=date("Y-m-d H:i:s");
    $where['flg']=input('flg');
    $res=db('bangcai')
      ->insert($where);
    if(!empty($res)){
      return $this->AjaxReturn("1","操作成功");
    }else{
      return $this->AjaxReturn("0","操作失败");
    }
  }
    /**
     * 获取群组
     */
  public function  getGroupList(){
      $arr=db('group_name')
          ->order('dingyueshu desc')
          ->select();
      if (!empty($sql)){
          return $this->AjaxReturn("1","操作成功",$arr);
      }else{
          return $this->AjaxReturn("-1","操作失败");
      }
  }
    /**
     * 视频上传
     */
  public function video_add(){
      $uid=$this->uid;
      if (request()->isPost()){
            $video = $_FILES['video'];
            $res = upload_file($video);
            $insert=input('post.');
            if(!is_numeric($res)){
                $file['path']=$res;
                $file['uid']=$uid;
                $file['number']=$insert['number'];
                $file['price']=$insert['price'];
                $file['address']=$insert['address'];
                $file['title']=$insert['title'];
                $file['content']=$insert['content'];
                $file['gid']=$insert['gid'];
                $file['type']=3;
                $file['createtime']=date('Y-m-d H:i:s');
                $isok=db::name('message')->insert($file);
                if($isok){
                    return $this->AjaxReturn('1',"插入成功");
                }
            }
        }
      return $this->AjaxReturn('0',"插入失败");
  }
  public function del(){
    $mid=input('mid');
    $photo=input('photo');
    $res=db('message')
      ->where('mid',$mid)
      ->field('picture')
      ->find();
    $arr=explode(",",$res['picture']);
    $arr=array_filter($arr); 
    for($i=0;$i<count($arr);$i++){
      if($arr[$i]==$photo){
        $arr[$i]='';
      }
    }

    $rs=implode(",",$arr);
    $stu=db('message')
      ->where('mid',$mid)
      ->update(['picture'=>$rs]);

    $fileRoute = ROOT_PATH . 'upload/message/'.$photo;
    //unlink($fileRoute);
    if(unlink($fileRoute)){
      return $this->AjaxReturn('1','成功',[$res,$fileRoute]);
    }else{
      return $this->AjaxReturn('2','失败');
    }
  }
  public function fangqi(){
    $mid=input(mid);
    $res=db()
      ->delete('mid',$mid);
    if($res){
      return $this->AjaxReturn('1','放弃发布');
    }
  } 
  public function delsql(){
    $mid= input('mid');
    if($mid != ''){
      $res= db('message')
        ->where('mid',$mid)
        ->find();
      if($res['createtime']==''){
        $arr=explode(",",$res['picture']);
        $arr = array_filter($arr);
        for($i=0;$i<count($arr);$i++){
          $fileRoute = ROOT_PATH . 'upload/message/'.$arr[$i];
          unlink($fileRoute);
        }
        $rs=db('message')
          ->where('mid',$mid)
          ->delete();
      }
      return $this->AjaxReturn('10','成功');
    }
  }
  public function panduan(){
    $uid=$this->uid;
    $times=date('Y-m-d 00:00:00');
    $timee=date('Y-m-d H:i:s');
    $where ['createtime'] = array("between",array($times,$timee));
    $where['uid']=$uid;
    $res=db('message')
      ->where($where)
      ->count();
    $k=db('shezhi')
      ->find();
    $rs=db('real')
      ->where("uid",$uid)
      ->field('tid,rflg')
      ->find();
    if($rs['rflg']!="1"&&$res>=$k['userwenzhang']){
      return $this->AjaxReturn("12","今日超出发布次数",$rs);
    }
    if($rs['rflg']=="1"&&$res>=$k['vipwenzhang']){
      return $this->AjaxReturn("22","今日超出发布次数");
    }
    return $this->AjaxReturn("1","发布正常");                 
  }
  public function Invitation(){
    $uid=$this->uid;
    $rs=db('real')
      ->where('uid',$uid)
      ->find();
    if($rs['rflg']==1){
      $res=db('user')
        ->where('uid',$uid)
        ->field('uid,uname,cishu,yaoqing,uflg')
        ->find();
      $res['yaoqing']=explode(',',$res['yaoqing']);
    }else{
      $res=db('user')
        ->where('uid',$uid)
        ->field('uid,nickname as uname,cishu,yaoqing,uflg')
        ->find();
      $res['yaoqing']=explode(',',$res['yaoqing']);
    }
    if($res){
      return $this->AjaxReturn("1","操作成功",$res);
    }else{
      return $this->AjaxReturn("-1","操作失败");
    }
  }

}